package com.zbkj.crmeb.merchant.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.common.CheckAdminToken;
import com.common.CommonPage;
import com.common.PageParamRequest;
import com.crmeb.core.common.CommonResult;
import com.crmeb.core.exception.CrmebException;
import com.github.pagehelper.PageInfo;
import com.utils.PriceUtil;
import com.zbkj.crmeb.chant.model.CategoryMatch;
import com.zbkj.crmeb.chant.request.ChannelProductBatchInfoRequest;
import com.zbkj.crmeb.chant.response.ChannelProductBatchInfoResponse;
import com.zbkj.crmeb.chant.response.ChannelProductBatchRecordResponse;
import com.zbkj.crmeb.chant.service.ChannelProductBatchInfoService;
import com.zbkj.crmeb.chant.service.ChannelProductBatchRecordService;
import com.zbkj.crmeb.chant.service.ICategoryMatchService;
import com.zbkj.crmeb.chant.service.IChannelProductService;
import com.zbkj.crmeb.enums.ProductTypeEnum;
import com.zbkj.crmeb.merchant.admin.service.MerStoreProductService;
import com.zbkj.crmeb.store.model.StoreProduct;
import com.zbkj.crmeb.store.model.StoreProductUpload;
import com.zbkj.crmeb.store.model.Supplier;
import com.zbkj.crmeb.store.request.*;
import com.zbkj.crmeb.store.response.StoreProductAttrValueResponse;
import com.zbkj.crmeb.store.response.StoreProductNormResponse;
import com.zbkj.crmeb.store.response.StoreProductResponse;
import com.zbkj.crmeb.store.response.StoreProductTabsHeader;
import com.zbkj.crmeb.store.service.*;
import com.zbkj.crmeb.store.vo.StoreProductMerVo;
import com.zbkj.crmeb.system.service.SystemConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

/**
 * 商品表 前端控制器
 *
 * @author muyu
 * @date 2020-10-13
 */
@RestController
@RequestMapping("api/admin/mer/store/product")
@Api(tags = "商品") //配合swagger使用
public class MerStoreProductController extends AdminBaseController {
    @Autowired
    PriceUtil priceUtil;
    @Autowired
    private MerStoreProductService storeProductService;
    @Autowired
    private StoreCartService storeCartService;
    @Autowired
    SystemConfigService systemConfigService;
    @Autowired
    private StoreBrandService storeBrandService;
    @Autowired
    private IChannelProductService channelProductService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    CheckAdminToken checkAdminToken;
    @Autowired
    StoreProductMerService storeProductMerService;
    @Autowired
    ICategoryMatchService categoryMatchService;
    @Autowired
    private StoreProductDraftService storeProductDraftService;
    @Autowired
    private ChannelProductBatchInfoService channelProductBatchInfoService;
    @Autowired
    private ChannelProductBatchRecordService channelProductBatchRecordService;

    /**
     * 分页显示商品表(供应商自建商品)
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "自建商品分页列表")
    @RequestMapping(value = "/inside/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<StoreProductResponse>> getInsideList(@Validated StoreProductSearchRequest request, @Validated PageParamRequest pageParamRequest, HttpServletRequest servletRequest) {
        request.setIsEurope(ProductTypeEnum.INSIDE.getCode());
        PageInfo<StoreProductResponse> pageInfo = storeProductService.getMerchnatList(request, pageParamRequest, getMerId(servletRequest));
        if (pageInfo == null) {
            return CommonResult.success(CommonPage.restPage(new ArrayList<>()));
        }

        return CommonResult.success(CommonPage.restPage(pageInfo));
    }

    /**
     * 分页显示商品表(供应商商品)
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "云仓商品分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<StoreProductResponse>> getList(StoreProductSearchRequest request, @Validated PageParamRequest pageParamRequest, HttpServletRequest servletRequest) {
        List<Integer> isNotEurope = Arrays.asList(ProductTypeEnum.INSIDE.getCode());
        request.setIsNotEurope(isNotEurope);

        PageInfo<StoreProductResponse> pageInfo = storeProductService.getMerchnatList(request, pageParamRequest, getMerId(servletRequest));
        if (pageInfo == null) {
            return CommonResult.success(CommonPage.restPage(new ArrayList<>()));
        }
        return CommonResult.success(CommonPage.restPage(pageInfo));
    }

    /**
     * 分页显示商品表(供应商商品)
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "云仓商品分页列表")
    @RequestMapping(value = "/list2", method = RequestMethod.GET)
    public CommonResult<CommonPage<StoreProductResponse>> getList2(StoreProductSearchRequest request, @Validated PageParamRequest pageParamRequest, HttpServletRequest servletRequest) {
        List<Integer> isNotEurope = Arrays.asList(ProductTypeEnum.INSIDE.getCode());
        request.setIsNotEurope(isNotEurope);

        PageInfo<StoreProductResponse> pageInfo = storeProductService.getMerchnatList2(request, pageParamRequest, getMerId(servletRequest));
        if (pageInfo == null) {
            return CommonResult.success(CommonPage.restPage(new ArrayList<>()));
        }
        return CommonResult.success(CommonPage.restPage(pageInfo));
    }


    @ApiOperation(value = "草稿箱商品列表")
    @RequestMapping(value = "/draft/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<StoreProductResponse>> getMerDraftList(StoreProductSearchRequest request, @Validated PageParamRequest pageParamRequest, HttpServletRequest servletRequest) {
        request.setIsEurope(ProductTypeEnum.INSIDE.getCode());
        PageInfo<StoreProductResponse> pageInfo = storeProductDraftService.getMerDraftList(request, pageParamRequest, getMerId(servletRequest));
        if (pageInfo == null) {
            return CommonResult.success(CommonPage.restPage(new ArrayList<>()));
        }
        return CommonResult.success(CommonPage.restPage(pageInfo));
    }

    /**
     * 商品sku列表
     *
     * @param @param  request
     * @param @param  pageParamRequest
     * @param @return
     * @return CommonResult<CommonPage < StoreProductResponse>>
     * @throws
     * @Description:
     * @author daigb
     */
    @ApiOperation(value = "sku分页列表")
    @GetMapping("/sku/page")
    public CommonResult<PageInfo<StoreProductAttrValueResponse>> selectSkuPage(@Validated StoreProductSearchRequest request) {
        PageInfo<StoreProductAttrValueResponse> pageInfo = storeProductService.selectSkuPage(request);
        return CommonResult.success(pageInfo);
    }

    /**
     * 新增商品表
     *
     * @param storeProductRequest 新增参数
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "新增")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<String> save(@RequestBody @Validated StoreProductRequest storeProductRequest, HttpServletRequest request) {
        if (storeProductRequest.getEarliestArrivalData() >= storeProductRequest.getSlowestArrivalData()) {
            throw new CrmebException("最慢到货天数必须大于最快到货天数");
        }
        Supplier supplier = supplierService.getSupplierBarcode(getMerId(request));
        if (null == supplier) {
            return CommonResult.failed("请给商户创建供应商后再添加商品!");
        }
        CategoryMatch categoryMatch = categoryMatchService.getById(storeProductRequest.getCateId());
        if (null == categoryMatch || Arrays.asList(categoryMatch.getPath().split("/")).size() != 4) {
            return CommonResult.failed("分类必须选择到三级！");
        }
        if (Objects.isNull(storeProductRequest.getWeight())) {
            storeProductRequest.setWeight(categoryMatch.getWeight());
        }
        //sourceCost
        storeProductRequest.setSuppliersId(supplier.getId());
        storeProductRequest.setSuppliersName(supplier.getSuppliersName());
        storeProductRequest.setMerId(getMerId(request));
//        storeProductRequest.setSourceCost(storeProductRequest.getCost());
        if (storeProductService.save(storeProductRequest)) {
            //获取品牌id
            Integer brandId = storeProductRequest.getBrandId();

            //处理品牌-在线商品数量
            storeBrandService.showProductCount(brandId, true);

            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "保存草稿箱")
    @RequestMapping(value = "/drafts/save", method = RequestMethod.POST)
    public CommonResult<String> saveDrafts(@RequestBody @Validated StoreProductRequest storeProductRequest, HttpServletRequest request) {
        if (storeProductRequest.getEarliestArrivalData() >= storeProductRequest.getSlowestArrivalData()) {
            throw new CrmebException("最慢到货天数必须大于最快到货天数");
        }
        Supplier supplier = supplierService.getSupplierBarcode(getMerId(request));
        if (null == supplier) {
            return CommonResult.failed("请给商户创建供应商后再添加商品!");
        }
        CategoryMatch categoryMatch = categoryMatchService.getById(storeProductRequest.getCateId());
        if (null == categoryMatch || Arrays.asList(categoryMatch.getPath().split("/")).size() != 4) {
            return CommonResult.failed("分类必须选择到三级！");
        }
        if (Objects.isNull(storeProductRequest.getWeight())) {
            storeProductRequest.setWeight(categoryMatch.getWeight());
        }
        //sourceCost
        storeProductRequest.setSuppliersId(supplier.getId());
        storeProductRequest.setSuppliersName(supplier.getSuppliersName());
        storeProductRequest.setMerId(getMerId(request));
        return storeProductService.saveDrafts(storeProductRequest) ? CommonResult.success() : CommonResult.failed();
    }

    /**
     * 修改商品表
     *
     * @param storeProductRequest 修改参数
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "修改自建商品")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestBody @Validated StoreProductRequest storeProductRequest, HttpServletRequest servletRequest) {
        if (storeProductRequest.getEarliestArrivalData() >= storeProductRequest.getSlowestArrivalData()) {
            throw new CrmebException("最慢到货天数必须大于最快到货天数");
        }
        StoreProduct storeProduct = storeProductService.getById(storeProductRequest.getId());
        if (null == storeProduct) {
            return CommonResult.failed();
        }
        CategoryMatch categoryMatch = categoryMatchService.getById(storeProductRequest.getCateId());
        if (null == categoryMatch || Arrays.asList(categoryMatch.getPath().split("/")).size() != 4) {
            return CommonResult.failed("分类必须选择到三级！");
        }
        if (Objects.isNull(storeProductRequest.getWeight())) {
            storeProductRequest.setWeight(categoryMatch.getWeight());
        }
        Integer merId = getMerId(servletRequest);
        if (ProductTypeEnum.INSIDE.getCode() != storeProduct.getIsEurope() && merId != storeProduct.getMerId()) {
            return CommonResult.failed("只能修改自己新增的商品！");
        }
        //sourceCost
        storeProductRequest.setMerId(merId);
        storeProductRequest.setSourceCost(storeProductRequest.getCost());
        if (storeProductService.update(storeProductRequest)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改云仓商品
     *
     * @param storeProductMerRequest 修改参数
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "修改云仓商品")
    @RequestMapping(value = "/update/cloud", method = RequestMethod.POST)
    public CommonResult<String> updateCloud(@RequestBody @Validated StoreProductMerRequest storeProductMerRequest, HttpServletRequest servletRequest) {
        Integer merId = getMerId(servletRequest);

        if (storeProductMerService.updateByCloud(storeProductMerRequest, merId)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 云仓商品批量加价
     *
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "云仓商品批量加价")
    @RequestMapping(value = "/raise/cloud", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestBody StoreProductMerCloudRequest request, HttpServletRequest servletRequest) {
        Integer merId = getMerId(servletRequest);
        if (CollectionUtils.isEmpty(request.getProductIds()) || request.getPrice().compareTo(BigDecimal.ZERO) == -1) {
            return CommonResult.failed();
        }
        // 判断是否减价
        if (request.getFormFlag()) {
            request.setPrice(request.getPrice().negate());
        }

        if (storeProductMerService.raiseByCloud(request.getProductIds(), request.getPrice(), merId)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 查询商品表信息
     *
     * @param id Integer
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "云仓商品详情")
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public CommonResult<StoreProductNormResponse> info(@PathVariable Integer id, HttpServletRequest servletRequest) {
        StoreProductNormResponse storeProductResponse = storeProductService.getSupplierByProductId(id, getMerId(servletRequest));
        return CommonResult.success(storeProductResponse);
    }

    /**
     * 查询商品表信息
     *
     * @param id Integer
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "自建商品详情")
    @RequestMapping(value = "/inside/info/{id}", method = RequestMethod.GET)
    public CommonResult<StoreProductResponse> insideInfo(@PathVariable Integer id, HttpServletRequest servletRequest) {
        StoreProductResponse storeProductResponse = storeProductService.getByProductId(id, getMerId(servletRequest), false, false, null, false);
        return CommonResult.success(storeProductResponse);
    }

    @ApiOperation(value = "草稿箱商品详情")
    @RequestMapping(value = "/draft/info/{id}", method = RequestMethod.GET)
    public CommonResult<StoreProductResponse> draftInfo(@PathVariable Integer id, HttpServletRequest servletRequest) {
        StoreProductResponse storeProductResponse = storeProductDraftService.draftInfo(id, getMerId(servletRequest));
        return CommonResult.success(storeProductResponse);
    }

    /**
     * 商品tabs表头数据
     *
     * @return
     */
    @ApiOperation(value = "供应商商品表头数量")
    @RequestMapping(value = "/tabs/headers", method = RequestMethod.GET)
    public CommonResult<List<StoreProductTabsHeader>> getTabsHeader(HttpServletRequest servletRequest) {
        return CommonResult.success(storeProductService.getTabsHeader(2, getMerId(servletRequest)));
    }

    /**
     * 商品tabs表头数据
     *
     * @return
     */
    @ApiOperation(value = "自建商品表头数量")
    @RequestMapping(value = "/inside/tabs/headers", method = RequestMethod.GET)
    public CommonResult<List<StoreProductTabsHeader>> getInsideTabsHeader(HttpServletRequest servletRequest) {
        return CommonResult.success(storeProductService.getTabsHeader(1, getMerId(servletRequest)));
    }

    /**
     * 上架
     *
     * @param id integer id
     * @author Mr.Zhang
     * @since 2020-05-06
     */
    @ApiOperation(value = "上架")
    @RequestMapping(value = "/putOnShell/{id}", method = RequestMethod.GET)
    public CommonResult<String> putOn(@PathVariable Integer id) {
        StoreProduct storeProduct = new StoreProduct();
        storeProduct.setId(id);
        storeProduct.setIsShow(true);
        if (storeProductService.updateById(storeProduct)) {
            //处理品牌-在线商品数量
            storeBrandService.showProductCount(storeProduct.getBrandId(), true);
            //更新关联的渠道商品
            channelProductService.updateProductByBarCode(storeProduct.getBarCode());
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 下架
     *
     * @param id integer id
     * @author Mr.Zhang
     * @since 2020-05-06
     */
    @ApiOperation(value = "下架")
    @RequestMapping(value = "/offShell/{id}", method = RequestMethod.GET)
    public CommonResult<String> offShell(@PathVariable Integer id) {
        StoreProduct storeProduct = storeProductService.getById(id);
        storeProduct.setIsShow(false);
        if (storeProductService.updateById(storeProduct)) {
            storeCartService.productStatusNotEnable(id);
            //处理品牌-在线商品数量
            storeBrandService.showProductCount(storeProduct.getBrandId(), false);
            //更新关联的渠道商品
            channelProductService.updateProductByBarCode(storeProduct.getBarCode());
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

//    /**
//     * 虚拟销量(已弃用)
//     * @param id integer id
//     * @author Mr.Zhang
//     * @since 2020-05-06
//     */
	/*@ApiOperation(value = "虚拟销量")
	@RequestMapping(value = "/ficti/{id}/{num}", method = RequestMethod.GET)
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "id", dataType = "int", required = true), @ApiImplicitParam(name = "num", value = "数值", dataType = "int", required = true), })
	public CommonResult<String> sale(@PathVariable Integer id, @PathVariable Integer num) {
	    StoreProduct storeProduct = storeProductService.getById(id);
	    storeProduct.setFicti(num);
	    if (storeProductService.updateById(storeProduct)) {
	        return CommonResult.success();
	    } else {
	        return CommonResult.failed();
	    }
	}*/

    @ApiOperation(value = "刷新商品索引")
    @RequestMapping(value = "/updateIndex", method = RequestMethod.GET)
    public CommonResult<String> updateIndex() {
//        ThreadUtil.execute(() -> {
//            productSearchService.updateData();
//        });
        return CommonResult.success("开始刷新");
    }

    @PutMapping("/is-show")
    @ApiOperation("禁用或启用")
    public CommonResult<Object> enabled(@RequestBody StoreProductMerVo vo, HttpServletRequest request) {
        String token = checkAdminToken.getTokenFormRequest(request);
        return CommonResult.success(storeProductMerService.enabled(token, vo));
    }

    @PutMapping("/is-best")
    @ApiOperation("小编推荐")
    public CommonResult<Object> best(@RequestBody StoreProductMerVo vo, HttpServletRequest request) {
        String token = checkAdminToken.getTokenFormRequest(request);
        return CommonResult.success(storeProductMerService.best(token, vo));
    }

    @PutMapping("/batch-is-show")
    @ApiOperation("批量上架/下架")
    public CommonResult<Object> batchEnabled(@RequestBody @Validated BatchEnabledRequest vo, HttpServletRequest request) {
        String token = checkAdminToken.getTokenFormRequest(request);
        return CommonResult.success(storeProductMerService.batchEnabled(token, vo));
    }

    @PutMapping("/rebate/edit/batch")
    @ApiOperation("批量修改佣金比例")
    public CommonResult<Object> batchEditRebateRate(@RequestBody @Validated BatchEditRebateRateRequest vo, HttpServletRequest request) {
        String token = checkAdminToken.getTokenFormRequest(request);
        return CommonResult.success(storeProductMerService.batchEditRebateRate(token, vo));
    }

    @PutMapping("/batch-is-best")
    @ApiOperation("批量推荐")
    public CommonResult<Object> batchBest(@RequestBody @Validated BestEnabledRequest vo, HttpServletRequest request) {
        String token = checkAdminToken.getTokenFormRequest(request);
        return CommonResult.success(storeProductMerService.batchBest(token, vo));
    }

    @PutMapping("/batch-update-price")
    @ApiOperation("批量修改价格")
    public CommonResult<Object> batchUpdatePrice(@RequestBody @Validated BatchUpdatePriceRequest request, HttpServletRequest httpServletRequest) {
        return storeProductMerService.batchUpdatePrice(request, getMerId(httpServletRequest));
    }

    @PutMapping("/batch-update-shipTemp")
    @ApiOperation("批量设置运费模板")
    public CommonResult<Object> batchUpdateShipTemp(@RequestBody @Validated BatchUpdateShipTempRequest request, HttpServletRequest httpServletRequest) {
        return storeProductMerService.batchUpdateShipTemp(request, getMerId(httpServletRequest));
    }

    /**
     * 分页显示商品表(供应商自建商品)
     *
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "通过ids查询商品列表")
    @RequestMapping(value = "/listByIds", method = RequestMethod.POST)
    public CommonResult<List<StoreProductResponse>> getListByIds(@RequestBody List<Integer> ids, HttpServletRequest servletRequest) {
        List<StoreProductResponse> list = storeProductService.getMerchnatList(ids, getMerId(servletRequest), false);
        if (list == null) {
            return CommonResult.success(new ArrayList<>());
        }
        return CommonResult.success(list);
    }


    @PutMapping("/resetPrice")
    @ApiOperation("重置为分类加价率")
    public CommonResult<Object> resetPrice(@RequestParam @ApiParam(value = "商品id", required = true) Integer productId, HttpServletRequest httpServletRequest) {
        return storeProductMerService.resetPrice(productId, getMerId(httpServletRequest));
    }

    @PutMapping("/batch/priceTail")
    @ApiOperation("价格尾号设置")
    public CommonResult<Object> batchPriceTail(@RequestBody @Validated BatchPriceTailRequest priceTailRequest, HttpServletRequest request) {
        return CommonResult.success(storeProductMerService.batchPriceTailNumber(getMerId(request), priceTailRequest));
    }

    @PutMapping("/batch/priceTailClear")
    @ApiOperation("价格尾号清除")
    public CommonResult<Object> batchPriceTailClear(@RequestBody @Validated BatchPriceTailRequest priceTailRequest, HttpServletRequest request) {
        priceTailRequest.setPriceTailNumber(null);
        return CommonResult.success(storeProductMerService.batchPriceTailNumberClear(getMerId(request), priceTailRequest));
    }

    @PutMapping("/transfer/cloud")
    @ApiOperation("全新自建商品批量申请转成云仓商品")
    public CommonResult<Object> transferCloud(@RequestBody Set<Integer> productIdList, HttpServletRequest httpServletRequest) {
        if (CollectionUtils.isEmpty(productIdList)) {
            return CommonResult.validateFailed("商品id不能为空");
        }
        return storeProductService.transferCloud(productIdList, getMerId(httpServletRequest)) ? CommonResult.success() : CommonResult.failed();
    }

    @GetMapping("/open/{productId}")
    @ApiOperation("展开商品sku")
    public CommonResult<List<StoreProductAttrValueResponse>> openProduct(@PathVariable("productId") Integer productId, HttpServletRequest httpServletRequest) {
        return CommonResult.success(storeProductService.openProduct(productId, getMerId(httpServletRequest)));
    }

    @GetMapping(value = "/upload/list")
    @ApiOperation(value = "上传记录")
    public CommonResult<CommonPage<StoreProductUpload>> uploadList(PageParamRequest request, HttpServletRequest servletRequest) {
        PageInfo<StoreProductUpload> pageInfo = storeProductService.uploadList(request, getMerId(servletRequest));
        return CommonResult.success(CommonPage.restPage(pageInfo));
    }

    @PostMapping(value = "/update/oneselfPrice")
    @ApiOperation(value = "自建商品修改零售价")
    public CommonResult<Boolean> updatePriceByOneself(@ApiParam("各规格价格列表") @RequestBody List<StoreProductOneselfRequest> requests) {
        return CommonResult.success(storeProductMerService.updatePriceByOneself(requests));
    }

    /**
     * 删除商品表
     *
     * @param id Integer
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public CommonResult<String> delete(@RequestBody @PathVariable Integer id) {
        if (storeProductService.deleteProduct(id)) {
            //        if(storeProductService.removeById(id)){
            storeCartService.productStatusNotEnable(id);
            StoreProduct storeProduct = storeProductService.getById(id);
            if (ObjectUtils.isNotEmpty(storeProduct)) {
                //处理品牌-在线商品数量
                storeBrandService.showProductCount(storeProduct.getBrandId(), false);
                //更新关联的渠道商品
                channelProductService.updateProductByBarCode(storeProduct.getBarCode());
            }
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "批量修改商品信息")
    @RequestMapping(value = "/batch", method = RequestMethod.POST)
    public CommonResult<Boolean> updateBatchProduct(@RequestBody List<ChannelProductBatchInfoRequest> requests, HttpServletRequest request) {
        return CommonResult.success(channelProductBatchInfoService.updateBatchProduct(requests, getMerId(request)));
    }

    @ApiOperation(value = "查询批量修改记录")
    @RequestMapping(value = "/batch", method = RequestMethod.GET)
    public CommonResult<CommonPage<ChannelProductBatchInfoResponse>> getList(ChannelProductBatchInfoRequest request, HttpServletRequest servletRequest) {
        return CommonResult.success(CommonPage.restPage(channelProductBatchInfoService.getList(request, getMerId(servletRequest))));
    }

    @ApiOperation(value = "查询记录详情")
    @RequestMapping(value = "/batch/info", method = RequestMethod.GET)
    public CommonResult<CommonPage<ChannelProductBatchRecordResponse>> getListByInfoId(@ApiParam("操作id") Integer infoId,
                                                                                       @ApiParam("查询状态 空：全部 0：失败 1：成功") Integer status,
                                                                                       PageParamRequest request) {
        return CommonResult.success(CommonPage.restPage(channelProductBatchRecordService.getListByInfoId(infoId, status, request)));
    }

    @ApiOperation(value = "查询操作人列表")
    @RequestMapping(value = "/batch/admin", method = RequestMethod.GET)
    public CommonResult<Map<Integer, String>> getOperationUserList(HttpServletRequest request) {
        return CommonResult.success(channelProductBatchInfoService.getOperationUserMap(getMerId(request)));
    }


    @ApiOperation(value = "商户商品表处理")
    @RequestMapping(value = "/doStoreProductMer", method = RequestMethod.POST)
    public CommonResult<String> doStoreProductMer(HttpServletRequest servletRequest) {

        Integer merId = getMerId(servletRequest);

        if (storeProductMerService.buildCNGoods(merId)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }
}
